package com.xctech.yace.util;

import com.xctech.filib.instrument.debtinst.DebtInstInfo;
import com.xctech.yace.enumeration.DimMapping;
import com.xctech.yace.manger.DimDataManager;
import com.xctech.yace.pojo.Interest;
import com.xctech.yace.pojo.PtyPdBaseInfo;
import com.xctech.yace.pojo.VarBondInfo;
import com.xctech.yace.pojo.VarIntrTaxPrefBond;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * @author xin.chu
 */
public class InterestTax {
    /**
     * 基于中台2.0模型构建债务工具对象 税前利息计算
     *
     * @param sidt 起息日期
     * @param matuDate 到期日期
     * @param yearIntpTims 年付息次数
     * @param intaBasi 计息基准 1->ACT/365,2->ACT/360,3->30E/360,4->ACT/ACT,5->NL/365F,6->AVG/ACT,7->NL/365,8->30/360,9->ACT/366,10->30U/360,11->30ST/360
     * @param issPrc 发行价
     * @param deno 面值
     * @param copn 年利率（小数）
     * @param intpType 付息类型 1->零息债券,2->附息债券,3->息票累积债券
     * @return
     */

    private static Logger logger = LoggerFactory.getLogger(InterestTax.class);

    /**
     * @param pdId       产品序号
     * @param scrIncd    证券内码
     * @param startOfDay 是否日初
     * @param val        计息日期
     * @return
     */
    public static Interest eval(Integer pdId, String scrIncd, boolean startOfDay, LocalDate... val) {

        Interest interestPojo = new Interest();
        VarBondInfo varBondInfo = null;
        try {
            Object objectBond = DimDataManager.getInstance().getRecord(DimMapping.VAR_BOND_INFO.getTable(), scrIncd);
            varBondInfo = (VarBondInfo) objectBond;
            //交收日期-
            LocalDate midDate = val[0];

            //税前利息计算
            DebtInstInfo debtInstInfo = DebtInstInfo.of(varBondInfo.getSIDT(), varBondInfo.getMATU_DATE(), new Double(varBondInfo.getYEAR_INTP_TIMS()).intValue(),
                    varBondInfo.getINTA_BASI(), varBondInfo.getISS_PRC(), varBondInfo.getDENO(), varBondInfo.getCOPN(),
                    varBondInfo.getINTP_TYPE());
            interestPojo.setPreInterest(new BigDecimal(debtInstInfo.calcAccrIntr(midDate, startOfDay)).setScale(12, BigDecimal.ROUND_HALF_UP).doubleValue());
            logger.info("债券{}单券税值{},详细参数{}", varBondInfo.getSCR_INCD(), interestPojo.getPreInterest(), varBondInfo);
        } catch (Exception e) {
            logger.error("根据key:{},获取债券信息异常:{}", scrIncd, e.getMessage());
        }

        if(varBondInfo == null){
            return new Interest(0.0,0.0);
        }

        PtyPdBaseInfo ptyPdBaseInfo = null;
        try {
            Object objectBase = DimDataManager.getInstance().getRecord(DimMapping.PTY_PD_BASE_INFO.getTable(), String.valueOf(pdId));
            //产品信息
            ptyPdBaseInfo = (PtyPdBaseInfo) objectBase;
            //当前产品利息税优惠类型
            Integer type = ptyPdBaseInfo.getINTR_TAX_PREF_TYPE();
            if (type == null || type == 1) {
                logger.info("利息税优惠类型为{}免税", type);
                return interestPojo;
            } else if (type == 2) {

                VarIntrTaxPrefBond varIntrTaxPrefBond = null;
                try {
                    Object objectTaxPref = DimDataManager.getInstance().getRecord(DimMapping.VAR_BOND_INTR_TAX_PREF.getTable(), scrIncd);
                    varIntrTaxPrefBond = (VarIntrTaxPrefBond) objectTaxPref;
                } catch (Exception e) {
                    logger.error("根据key:{},获取债券利息优惠信息异常:{}", scrIncd, e.getMessage());
                }
                //优惠比例
                if (varIntrTaxPrefBond.getTAX_RATE() == null) {
                    varIntrTaxPrefBond.setTAX_RATE(0.2);
                    logger.info("优惠比例为{}默认按照比例20%处理", varIntrTaxPrefBond.getTAX_RATE());
                    interestPojo.setInterestTaxSum(new BigDecimal(interestPojo.getPreInterest() * varIntrTaxPrefBond.getTAX_RATE()).setScale(12, BigDecimal.ROUND_HALF_UP).doubleValue());
                    return interestPojo;
                } else {
                    interestPojo.setInterestTaxSum(new BigDecimal(interestPojo.getPreInterest() * varIntrTaxPrefBond.getTAX_RATE()).setScale(12, BigDecimal.ROUND_HALF_UP).doubleValue());
                    logger.info("利息税金额为{}", interestPojo.getInterestTaxSum());
                    return interestPojo;
                }
            }
        } catch (Exception e) {
            logger.error("根据key:{},获取产品信息异常:{}", pdId, e.getMessage());
        }

        return interestPojo;
    }
}
